<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Spot_Ticket extends ORM
{

    protected $_table_name = 'spot_ticket';

    /**
     * 无需提前
     */
    const IN_ADVANCE_BOOK_TYPE_NONE = 1;

    /**
     * 预订成功后
     */
    const IN_ADVANCE_BOOK_USE = 2;

    /**
     * 游玩前
     */
    const IN_ADVANCE_BOOK_TYPE_BEFORE = 3;

    /**
     * 游玩前&预订成功后
     */
    const IN_ADVANCE_BOOK_TYPE_BEFORE_USE = 4;

    /**
     * 需判断提前预订的类型
     */
    const IN_ADVANCE_BOOK_BEFORE = array(
        self::IN_ADVANCE_BOOK_TYPE_BEFORE,
        self::IN_ADVANCE_BOOK_TYPE_BEFORE_USE
    );

    public function delete_clear()
    {
        if (!empty($this->id)) {
            DB::delete('spot_ticket_price')
                ->where('ticketid', '=', $this->id)
                ->and_where('supplier_channel_id', '=', 1)
                ->execute();

            DB::delete('spot_ticket_number')
                ->where('suit_id', '=', $this->id)
                ->execute();

            DB::delete('spot_refund_rule')->where('suit_id', '=', $this->id)->execute();

            // 删除已经添加的规格
            DB::delete('spot_ticket_sku')->where('ticket_id', '=', $this->id)->execute();

            // 删除人群规则
            DB::delete('spot_ticket_people_rule')->where('ticket_id', '=', $this->id)->execute();

            $this->delete();
        }
    }

    /**
     * 添加套餐
     * @param $spot_id
     * @param $suit_name
     * @return true[]
     * @throws Exception
     */
    public static function add_suit($spot_id, $suit_name)
    {
        $orm                    = ORM::factory('spot_ticket');
        $orm->spotid            = $spot_id;
        $orm->title             = $suit_name;
        $orm->status            = 2;
        $orm->addtime           = time();
        $orm->auto_close_time   = 86400;
        $orm->auto_confirm_time = 86400;
        $orm->is_edited         = 0;
        $orm->save();
        if (!$orm->saved()) {
            throw new Exception('保存失败');
        }

        $orm->reload();
        $ticket_id = $orm->id;

        // 处理产品规格
        Model_Spot_Ticket_Sku::add_sku($spot_id, $ticket_id, 'ticket');

        return array('status' => true, 'data' => array('suit_id' => $ticket_id, 'suit_name' => $suit_name));
    }

    /**
     * 删除套餐
     * @param $ticket_id
     * @return array
     * @throws Exception
     */
    public static function del_suit($ticket_id)
    {
        $ticket = ORM::factory('spot_ticket', $ticket_id);
        if (empty($ticket->id)) {
            throw new Exception('门票不存在，刷新后重试');
        }

        $ticket->delete_clear();

        return array('status' => true, 'msg' => '保存成功');
    }

    /**
     * 获取后台套餐列表筛选条件
     * @return array
     */
    public static function get_suit_filter($spot_id)
    {
        $tickets = DB::select('id', 'title')
            ->from('spot_ticket')
            ->where('spotid', '=', $spot_id)
            ->and_where('status', 'in', array(1, 2, 3))
            ->execute()
            ->as_array();
        $data    = array();
        $data[]  = array(
            'name'  => 'ticket',
            'title' => '套餐',
            'width' => 240,
            'item'  => $tickets
        );

        // 有套餐才进行后续筛选条件展示
        if (!empty($tickets)) {
            $period_times = Model_Spot_Ticket_Period_Time::get_spot_period_time($spot_id);
            if (!empty($period_times)) {
                foreach ($period_times as &$item) {
                    $item = array(
                        'id'    => $item['period_time_id'],
                        'title' => $item['date_str'],
                    );
                }
                $data[] = array(
                    'name'  => 'period_time',
                    'title' => '场次',
                    'width' => 600,
                    'item'  => $period_times
                );
            }
        }
        $data[] = array(
            'name'  => 'status',
            'title' => '上下架',
            'width' => 80,
            'item'  => array(
                array(
                    'id'    => '1',
                    'title' => '审核中'
                ),
                array(
                    'id'    => '2',
                    'title' => '下架中'
                ),
                array(
                    'id'    => '3',
                    'title' => '上架中'
                ),
            )
        );

        return $data;
    }

    /**
     * 套餐列表
     * @param $spot_id
     * @param $params
     * @return array
     */
    public static function get_suit_list($spot_id, $params = array())
    {
        $ticket_id       = Arr::get($params, 'ticket_id', '');
        $period_time_id  = Arr::get($params, 'period_time_id', '');
        $settlement_type = Arr::get($params, 'settlement_type', '');
        $status          = Arr::get($params, 'status', '');
        $ticket_id       = is_array($ticket_id) ? $ticket_id : array($ticket_id);

        // 筛选时段中包含的门票
        if ($period_time_id) {
            $period_time_ticket_builder = DB::select()
                ->from('spot_ticket_sku')
                ->where('spot_id', '=', $spot_id);

            if (is_array($period_time_id)) {
                $period_time_ticket_builder->where('period_time_id', 'in', $period_time_id);
            } else {
                $period_time_ticket_builder->where('period_time_id', '=', $period_time_id);
            }

            $period_time_ticket = $period_time_ticket_builder->group_by('ticket_id')->execute()->as_array('id', 'ticket_id');
            if (!empty($period_time_ticket)) {
                $ticket_id = array_merge($ticket_id, $period_time_ticket);
            }
        }

        // 筛选门票
        $builder = DB::select('id', 'title', 'status', 'is_edited', 'settlement_type', 'displayorder')
            ->from('spot_ticket')
            ->where('spotid', '=', $spot_id);

        if (!empty($ticket_id)) {
            $builder->where('id', 'in', $ticket_id);
        }

        if (!empty($status)) {
            if (is_array($status)) {
                $builder->where('status', 'in', $status);
            } else {
                $builder->where('status', '=', $status);
            }
        }

        if (!empty($settlement_type)) {
            if (is_array($settlement_type)) {
                $builder->where('settlement_type', 'in', $settlement_type);
            } else {
                $builder->where('settlement_type', '=', $settlement_type);
            }
        }

        $tickets = $builder->order_by('id', 'desc')->execute()->as_array();


        if (!empty($tickets)) {
            // 判断是否有分时时段
            $has_period_time = self::has_period_time($spot_id);

            foreach ($tickets as &$ticket) {
                $extend_data = array();

                if ($has_period_time) {
                    $ticket_period_times    = self::get_ticket_period_time($spot_id, $ticket['id']);
                    $ticket_period_time_arr = array();
                    foreach ($ticket_period_times as $ticket_period_time) {
                        $ticket_period_time_arr[] = $ticket_period_time['start'] . '-' . $ticket_period_time['end'];
                    }
                    $extend_data[] = array('name' => 'period_time', 'value' => implode(',', $ticket_period_time_arr));
                }
                $ticket['displayorder'] = $ticket['displayorder']=='9999'?'':$ticket['displayorder'];
                $ticket['extend_data'] = $extend_data;
            }
        }
        return $tickets;
    }

    /**
     * 检查是否有时段
     * @param $spot_id
     * @return bool
     */
    public static function has_period_time($spot_id)
    {
        return DB::select()
            ->from(array('spot_ticket_period_time', 'a'))
            ->join(array('spot_period_time', 'b'), 'inner')
            ->on('a.period_time_id', '=', 'b.id')
            ->where('a.spot_id', '=', $spot_id)
            ->execute()
            ->valid();
    }

    /**
     * 获取门票的时段
     * @param $spot_id
     * @param string $ticket_id
     * @return array
     */
    public static function get_ticket_period_time($spot_id, $ticket_id = '')
    {
        $builder = DB::select('a.period_time_id', 'b.start', 'b.end')
            ->from(array('spot_ticket_sku', 'a'))
            ->join(array('spot_period_time', 'b'), 'inner')
            ->on('a.period_time_id', '=', 'b.id')
            ->where('a.spot_id', '=', $spot_id);
        if ($ticket_id) {
            $builder->where('a.ticket_id', '=', $ticket_id);
        }
        return $builder->execute()->as_array();
    }

    /**
     * 保存门票状态
     * @param $status
     * @param $ticket_id
     * @return array
     */
    public static function save_ticket_status($status, $ticket_id)
    {
        $data    = array(
            'status' => $status,
            'modtime' => time()
        );
        $builder = DB::update('spot_ticket');
        if (is_array($ticket_id)) {
            $builder->where('id', 'in', $ticket_id);
        } else {
            $builder->where('id', '=', $ticket_id);
        }
        $builder->set($data)->execute();

        return array('status' => true, 'msg' => '保存成功');
    }

    public static function delete_ticket($ticket_id)
    {
        DB::delete('spot_ticket')->where('id', 'in', $ticket_id)->execute();
        DB::delete('spot_ticket_number')->where('suit_id', 'in', $ticket_id)->execute();
        DB::delete('spot_ticket_people_rule')->where('ticket_id', 'in', $ticket_id)->execute();
        DB::delete('spot_ticket_stock')->where('ticket_id', 'in', $ticket_id)->execute();
        DB::delete('spot_ticket_price')->where('ticketid', 'in', $ticket_id)->execute();

        return array('status' => true, 'msg' => '删除成功');
    }

    /**
     * 保存门票结算方式
     * @param $settlement_type int 1结算价模式  2销售价模式
     * @param $ticket_id
     * @return array
     */
    public static function save_ticket_settlement_type($settlement_type, $ticket_id)
    {
        $data    = array(
            'settlement_type' => $settlement_type,
            'modtime' => time()
        );
        $builder = DB::update('spot_ticket');
        if (is_array($ticket_id)) {
            $builder->where('id', 'in', $ticket_id);
        } else {
            $builder->where('id', '=', $ticket_id);
        }
        $builder->set($data)->execute();

        // 销售价模式需要清空报价中的利润
        if ($settlement_type == 2) {
            Model_Price_Settlement::suit_price_modify(Model_Spot::$typeid, $ticket_id, $settlement_type);
        }

        return array('status' => true, 'msg' => '保存成功');
    }

    /**
     * 获取编辑信息
     * @param $ticket_id
     * @return array|boolean
     */
    public static function get_edit_info($ticket_id)
    {
        $info = ORM::factory('spot_ticket', $ticket_id)->as_array();
        if (empty($info['id'])) {
            return false;
        }
        $spot_id = $info['spotid'];

        $spot = DB::select('is_dist_people', 'is_open_multiple_ticket')
            ->from('spot')
            ->where('id', '=', $spot_id)
            ->execute()
            ->current();

        // 供应商信息
        $supplier = false;
        if (!empty($info['supplierlist'])) {
            $supplier = DB::select('id', 'suppliername', 'linkman', 'mobile')
                ->from('supplier')
                ->where('id', '=', $info['supplierlist'])
                ->execute()
                ->current();
        }
        $info['supplier'] = $supplier;

        // 门票类型
        $info['ticket_type_list'] = ORM::factory('spot_ticket_type')
            ->where('spotid', '=', $spot_id)
            ->order_by('displayorder', 'asc')
            ->get_all();

        if ($spot['is_open_multiple_ticket'] != 1) {
            // 全部预约时段
            $info['period_time_list'] = Model_Spot_Ticket_Period_Time::get_spot_period_time($spot_id);
            // 选择的预约时段
            $ticket_period_time_ids  = [];
            $ticket_period_time_list = Model_Spot_Ticket::get_ticket_period_time($spot_id, $ticket_id);
            if (!empty($ticket_period_time_list)) {
                foreach ($ticket_period_time_list as $ticket_period_time) {
                    $ticket_period_time_ids[] = $ticket_period_time['period_time_id'];
                }
            }
            $info['ticket_period_time_ids'] = $ticket_period_time_ids;
        }

        // 联系人填写
        $info['fill_link_man'] = $info['fill_link_man'] ? explode(',', $info['fill_link_man']) : array();

        // 预约提前时间
        $hour                = $info['hour_before'] < 10 ? '0' . $info['hour_before'] : $info['hour_before'];
        $minute              = $info['minute_before'] < 10 ? '0' . $info['minute_before'] : $info['minute_before'];
        $info['time_before'] = $hour . ':' . $minute;

        $info['fill_tourer_items_arr'] = explode(',', $info['fill_tourer_items']);


        if (empty($info['lastoffer'])) {
            $info['lastoffer'] = array('pricerule' => 'all', 'number' => '-1');
        }
        $info['auto_close_time_hour'] = floor($info['auto_close_time'] / 3600);;
        $info['auto_close_time_minute'] = floor(($info['auto_close_time'] - ($info['auto_close_time_hour'] * 3600)) / 60);

        $info['auto_confirm_time_hour'] = floor($info['auto_confirm_time'] / 3600);;
        $info['auto_confirm_time_minute'] = floor(($info['auto_confirm_time'] - ($info['auto_confirm_time_hour'] * 3600)) / 60);

        $info['no_confirm_refund_hour']   = floor($info['no_confirm_refund'] / 3600);
        $info['no_confirm_refund_minute'] = floor(($info['no_confirm_refund'] - ($info['no_confirm_refund_hour'] * 3600)) / 60);

        // 处理门票人群规则
        $people_rule = '';
        if ($spot['is_dist_people'] == 1) {
            $people_rule = Model_Spot_Ticket_People_Rule::get_people_rules($spot_id, $ticket_id);
        }
        $info['people_rule'] = $people_rule;

        // 抽佣方式
        $settlement_config = DB::select()
            ->from('supplier_settlement_config')
            ->where('type_id', '=', Model_Spot::$typeid)
            ->and_where('suit_id', '=', $ticket_id)
            ->and_where('channel_id', '=', 1)
            ->execute()
            ->current();

        if ($settlement_config) {
            $info['settlement_key'] = $settlement_config['settlement_key'] ?? 1;
            $info['settlement_val'] = $settlement_config['settlement_key'] == 1 ? bcmul($settlement_config['settlement_val'], 100) : $settlement_config['settlement_val'];
        }

        return $info;
    }

    /**
     * 保存门票信息
     * @param $params
     * @return array|void
     */
    public static function save_ticket($params)
    {
        try {
            $ticket_id = Arr::get($params, 'ticket_id');
            $model     = ORM::factory('spot_ticket', $ticket_id);
            if (empty($model->id)) {
                throw new Exception('门票不存在');
            }
            $supplier_id = Arr::get($params, 'supplier_id', '');

            $title          = Arr::get($params, 'title');
            $ticket_type_id = Arr::get($params, 'ticket_type_id');
            $sell_price     = Arr::get($params, 'sellprice', 0);
            $get_ticket_way = Arr::get($params, 'get_ticket_way', '');
            $description    = Arr::get($params, 'description', '');
            $at_least       = Arr::get($params, 'at_least', '');

            // 预定规则
            $in_advance_book_type = Arr::get($params, 'in_advance_book_type', 1);
            $hour_after           = Arr::get($params, 'hour_after', 0);
            $minute_after         = Arr::get($params, 'minute_after', 0);
            $day_before           = Arr::get($params, 'day_before', 0);
            $time_before          = Arr::get($params, 'time_before', '');
            $hour_before          = 0;
            $minute_before        = 0;
            if (!empty($time_before)) {
                $time_before_arr = explode(':', $time_before);
                $hour_before     = empty($time_before_arr[0]) ? 0 : intval($time_before_arr[0]);
                $minute_before   = empty($time_before_arr[1]) ? 0 : intval($time_before_arr[1]);
            }

            $effective_days = Arr::get($params, 'effective_days');

            $pay_type = Arr::get($params, 'paytype', 1);
            $ding_jin = Arr::get($params, 'dingjin', 0);

            $is_open_member_limit    = Arr::get($params, 'is_open_member_limit', 0);
            $member_limit_order_num  = Arr::get($params, 'member_limit_order_num', 0);
            $limit_order_num         = Arr::get($params, 'limit_order_num', 0);
            $is_open_limit_order_num = Arr::get($params, 'is_open_limit_order_num', 0);

            //预订确认方式
            $need_confirm = Arr::get($params, 'need_confirm', 0);

            //待付款时限,默认不限制(分钟小时制)
            $auto_close_time_minute = Arr::get($params, 'auto_close_time_minute', 0);
            $auto_close_time_hour   = Arr::get($params, 'auto_close_time_hour', 0);
            $auto_close_time        = $auto_close_time_hour * 3600 + $auto_close_time_minute * 60;

            $auto_confirm_time_minute = Arr::get($params, 'auto_confirm_time_minute', 0);
            $auto_confirm_time_hour   = Arr::get($params, 'auto_confirm_time_hour', 0);
            $auto_confirm_time        = $auto_confirm_time_hour * 3600 + $auto_confirm_time_minute * 60;

            //会员支付方式
            if ($pay_type != 2) {
                $ding_jin = 0;
                $pay_way  = array_sum(Arr::get($params, 'pay_way'));
            } else {
                $pay_way = 1; //定金支付只支持线上支付
            }

            if($is_open_limit_order_num==1 && $limit_order_num<$at_least){
                throw new Exception('起售应小于限购数量');
            }

            if($at_least<1){
                throw new Exception('起售数应大于0');
            }

            // 退改规则
            $refund_restriction = Arr::get($params, 'refund_restriction');

            $fill_link_man = Arr::get($params, 'fill_link_man', '');
            $fill_link_man = $fill_link_man && is_array($fill_link_man) ? implode(',', $fill_link_man) : '';

            $fill_tourer_type            = Arr::get($params, 'fill_tourer_type', 0);
            $fill_tourer_credential_type = Arr::get($params, 'fill_tourer_credential_type', 1);

            // 结算模式
            $settlement_type = Arr::get($params, 'settlement_type', 1);
            $settlement_key  = Arr::get($params, 'settlement_key', 1); // 1百分比 2固定金额
            $settlement_val  = Arr::get($params, 'settlement_val', 0); // 比例或固定金额
            $settlement_type = $supplier_id ? $settlement_type : 1; // 不是供应商时使用结算价报价模式

            $model->modtime       = time();
            $model->title         = $title;
            $model->tickettypeid  = $ticket_type_id;
            $model->sellprice     = $sell_price;
            $model->day_before    = $day_before;
            $model->hour_before   = $hour_before;
            $model->minute_before = $minute_before;

            // 结算
            $model->settlement_type = $settlement_type;

            $model->paytype      = $pay_type;
            $model->description  = $description;
            $model->at_least  = $at_least;
            $model->dingjin      = $ding_jin;
            $model->supplierlist = $supplier_id;

            $model->need_confirm    = $need_confirm;
            $model->pay_way         = $pay_way;
            $model->auto_close_time = $auto_close_time;
            $model->get_ticket_way  = $get_ticket_way;
            $model->effective_days  = $effective_days;

            $model->refund_restriction   = $refund_restriction;
            $model->auto_confirm_time    = $auto_confirm_time;
            $model->in_advance_book_type = $in_advance_book_type;

            $model->hour_after   = $hour_after;
            $model->minute_after = $minute_after;

            $model->fill_tourer_credential_type = $fill_tourer_credential_type;
            $model->fill_link_man               = $fill_link_man;
            $model->fill_tourer_type            = $fill_tourer_type;

            $model->member_limit_order_num  = $member_limit_order_num;
            $model->is_open_member_limit    = $is_open_member_limit;
            $model->limit_order_num         = $limit_order_num;
            $model->is_open_limit_order_num = $is_open_limit_order_num;
            $model->is_edited               = 1;
            $model->save();

            // 处理退款规则
            Model_Spot_Refund_Rule::get_instance()->save_refund_rule($model->spotid, $ticket_id, $params);

            // 处理预约时段
            $period_time_id = Arr::get($params, 'period_time_id', '');
            $period_time_id = !empty($period_time_id) ? $period_time_id : array();
            Model_Spot_Ticket_Sku::save_ticket_sku_period_time($model->spotid, $ticket_id, $period_time_id);

            // 处理人群规则
            Model_Spot_Ticket_People_Rule::save_rule($model->spotid, $ticket_id, $params);

            // 更新结算方式
            if ($settlement_type == 2) {
                Model_Price_Settlement::suit_price_modify(Model_Spot::$typeid, $ticket_id, $settlement_type);
                $settlement_params = array(
                    'settlement_type' => $settlement_type,
                    'type_id' => Model_Spot::$typeid,
                    'suit_id' => $ticket_id,
                    'channel_id' => 1,
                    'settlement_key' => $settlement_key,
                    'settlement_val' => $settlement_val,
                );
                Model_Price_Settlement::suit_modify($settlement_params);
            }
            return array('status' => true, 'msg' => '保存成功');
        } catch (Exception $e) {
            return array('status' => false, 'msg' => $e->getMessage());
        }
    }


    /**
     * 获取可预订的最低时间（用于报价获取）
     * @param $ticket_info array in_advance_book_type  day_before hour_before minute_before
     * @return string 无需提前的返回空字符串
     */
    public static function get_before_time($ticket_info)
    {
        $before_time = '';

        $in_advance_book_type = array(
            Model_Spot_Ticket::IN_ADVANCE_BOOK_TYPE_BEFORE,
            Model_Spot_Ticket::IN_ADVANCE_BOOK_TYPE_BEFORE_USE
        );

        if (in_array($ticket_info['in_advance_book_type'], $in_advance_book_type)) {
            $now_time      = time();
            $day_before    = intval($ticket_info['day_before']);
            $hour_before   = intval($ticket_info['hour_before']);
            $minute_before = intval($ticket_info['minute_before']);
            if ($day_before != 0 || $hour_before != 0 || $minute_before != 0) {
                if ($hour_before == 0 && $minute_before == 0) {
                    $offset = 24 * 3600;
                } else {
                    $offset = 3600 * $hour_before + $minute_before * 60;
                }
                $before_time = $now_time + $day_before * 24 * 3600 - $offset;
            }
        }

        return $before_time;
    }

    /**
     * 获取预订的提前预订时间（用于订单预定时间提示）
     * @param $ticket_info array in_advance_book_type  day_before hour_before minute_before
     * @return string 无需提前的返回空字符串
     */
    public static function get_before_time_tips($ticket_info)
    {
        $before_time = '';

        $in_advance_book_type = array(
            Model_Spot_Ticket::IN_ADVANCE_BOOK_TYPE_BEFORE,
            Model_Spot_Ticket::IN_ADVANCE_BOOK_TYPE_BEFORE_USE
        );

        if (in_array($ticket_info['in_advance_book_type'], $in_advance_book_type)) {
            $day_before    = intval($ticket_info['day_before']);
            $hour_before   = intval($ticket_info['hour_before']);
            $minute_before = intval($ticket_info['minute_before']);
            if ($day_before != 0 || $hour_before != 0 || $minute_before != 0) {
                $before_time = $day_before > 0 ? '需提前' . $day_before . '天' : '当天';
                if ($hour_before != 0 || $minute_before != 0) {
                    $hour_before   = sprintf('%02d', $hour_before);
                    $minute_before = sprintf('%02d', $minute_before);
                    $before_time   .= $hour_before . ':' . $minute_before . '前';
                }
                $before_time .= '预订';
            }
            $before_time = $ticket_info['title'] . $before_time;
        }

        return $before_time;
    }

    /**
     * 获取在售的门票 $spot_id || $ticket_id 必传一个
     * @param array $params $spot_id 景点id; $ticket_id 有门票id时返回一维数组; $key 使用$key字段的值返回数组；$filed 返回指定字段
     * @return array|mixed|null
     * @throws Exception
     */
    public static function get_on_sale_ticket($params)
    {
        $spot_id   = Arr::get($params, 'spot_id', '');
        $ticket_id = Arr::get($params, 'ticket_id', '');
        $key       = Arr::get($params, 'key', '');
        $filed     = Arr::get($params, 'filed', '*');
        if (!$spot_id && !$ticket_id) {
            throw new Exception('参数错误');
        }
        $builder = DB::select($filed)
            ->from('spot_ticket');
        if ($ticket_id) {
            $builder->where('id', '=', $ticket_id);
        }
        if ($spot_id) {
            $builder->where('spotid', '=', $spot_id);
        }
        $builder->and_where('status', '=', 3)
            ->and_where('is_edited', '=', 1);
        if ($ticket_id) {
            return $builder->execute()->current();
        } else {
            if ($key) {
                return $builder->execute()->as_array($key);
            } else {
                return $builder->execute()->as_array();
            }
        }
    }

    /**
     * 门票审核
     * @param $ticket_id
     * @param $status
     * @param $refuse_msg
     * @return bool
     */
    public static function audit_ticket($ticket_id, $status, $refuse_msg)
    {
        $data = array(
            'status' => $status,
            'refuse_msg' => $refuse_msg,
            'modtime' => time(),
        );

        $flag = DB::update('spot_ticket')
            ->set($data)
            ->where('id', '=', $ticket_id)
            ->execute();
        return (bool)$flag;
    }


    public static function get_settlement_type($ticket_id)
    {
        $settlement_type = DB::select()
            ->from('supplier_settlement_config')
            ->where('channel_id','=',1)
            ->and_where('suit_id','=',$ticket_id)
            ->execute()->current();
        $settlement_type['settlement_val'] = $settlement_type['settlement_key']==2?:bcmul($settlement_type['settlement_val'],100);

        return $settlement_type;
    }
}